/**
 *
 */
package com.evil.DB_Utils;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/******************************************
 * 工程名：Download<br>
 * 包名：com.evil.DB_Utils<br>
 * 时间：2016年6月10日 下午2:50:08<br>
 * 作者：白雨濃 evil<br>
 * 邮箱：evilbai@foxmail.com<br>
 * 内容：删除<br>
 ******************************************/
public class Delete extends DB_Utils
{
    private PreparedStatement preparedStatement;
    private String url;

    private int index;
    private String returnError;

    public Delete()
    {
    }

    public Delete(String url)
    {
        this.url = url;
        if (url.equals(""))
            System.err.println("url地址不能为空");
        else
        {
            try
            {
                preparedStatement = (PreparedStatement) connection
                        .prepareStatement(url);
            }
            catch (SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public Delete bulid(String dbName, String[] name, Object[] values)
    {
        String url = "DELETE  FROM " + dbName + " WHERE ";

        for (int i = 0; i < name.length; i++)
        {
            if (i == 0)
                url += name[i] + "=?";
            else
                url += "," + name[i] + "=?";
        }

        try
        {
            preparedStatement = (PreparedStatement) connection
                    .prepareStatement(url);

            for (int i = 0; i < values.length; i++)
            {
                preparedStatement.setObject(i + 1, values);
            }
            returnError = preparedStatement.executeUpdate() + "";
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

        return this;
    }

    public Delete bulid(String tableName, String name, Object value)
    {
        String url = "DELETE  FROM " + tableName + " WHERE ";

        url += name + "=?";

        try
        {
            preparedStatement = (PreparedStatement) connection
                    .prepareStatement(url);

            preparedStatement.setObject(1, value);
            returnError = preparedStatement.executeUpdate() + "";
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return this;
    }

    public String close()
    {
        try
        {
            if (preparedStatement != null)
                preparedStatement.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return returnError;
    }

}
